<template>
<div class="black_transparent_background">
  <div class="order_content">
    <div class="top">
      <span class="title">{{title}}</span>
      <span class="el-icon-close close" @click="close"></span>
    </div>
    <div class="mid">
      <div class="food_price">
        <p>￥{{price}}</p>
        <p>
          <el-rate
            v-model="rate"
            disabled
            text-color="#ff8400"
            score-template="{rate}">
          </el-rate>
        </p>
      </div>
      <div class="count">
        <button class="iconfont" @click="count<=1?count:count--">&#xe605;</button><span>{{count}}</span><button class="iconfont" @click="count++">&#xe603;</button>
      </div>
    </div>
    <textarea name="" id="" cols="30" rows="10" style="width: 538px;hight: 140px" v-model="remarks" placeholder="请输入备注"></textarea>
    <div class="bottom">
      <button class="cancle" @click="close">取消</button>
      <button class="determine" @click="submit">确定</button>
    </div>
  </div>
</div>
</template>

<script>
// import bus from '../../eventBus.js'
import { getSeatInfoAPI } from '@/api/getSeatInfoAPI.js'
import { setOrderDetailAPI } from '@/api/setOrderDetailAPI.js'
import { getOrderDetailAPI } from '@/api/getOrderDetailAPI.js'
export default {
  name: 'addDish',
  props: ['title', 'price', 'image', 'rate'],
  inject: ['reload'],
  data () {
    return {
      count: 1,
      remarks: '',
      isOrder: -1,
      table_num: ''
      // orderList: []
    }
  },
  methods: {
    close () {
      this.$emit('close', this.isOrder)
    },
    async submit () {
      if (this.table_num === '') return alert('请先选择桌子')
      this.close()
      const token = localStorage.getItem('token')
      const data = {
        table_num: this.table_num,
        dish_name: this.title,
        dish_img: this.image,
        unit_price: this.price,
        dish_count: this.count,
        remarks: this.remarks
      }
      await setOrderDetailAPI(token, data)
    },
    async getSeatInfo () {
      const token = localStorage.getItem('token')
      const { data: res } = await getSeatInfoAPI(token)
      const currentTable = res.data.find(item => item.is_selected === 1)
      if (!currentTable) return
      this.table_num = currentTable.seat_id
    },
    async getOrderDetail () {
      const token = localStorage.getItem('token')
      const { data: res } = await getOrderDetailAPI(token)
      const currentDish = res.data.find(item => item.table_num === this.table_num && item.dish_name === this.title)
      if (!currentDish) return
      this.count = currentDish.dish_count
      this.remarks = currentDish.remarks
    }
  },
  created () {
    this.getSeatInfo()
    this.getOrderDetail()
  }
}
</script>

<style scoped lang="less">
.order_content {
  position: fixed;
  left: 700px;
  top: 300px;
  width: 550px;
  height: 305px;
  background-color: #fff;
  border-radius: 5px;
  padding: 10px;
  z-index: 5;
  .top {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .title {
      font-size: 20px;
    }
    .close {
      color: rgb(136, 136, 136);
      font-size: 20px;
      cursor: pointer;
    }
    .close:hover {
      color: #333;
    }
  }
  .mid {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 10px;
    .food_price {
      p {
        color: #ff9900;
        font-weight: 700;
      }
    }
    .count {
      display: flex;
      justify-content: center;
      align-items: center;
      span {
        display: block;
        width: 20px;
        height: 18px;
        border: 1px solid #ff8400;
        line-height: 15px;
        text-align: center;
        font-size: 12px;
        color: rgb(133, 133, 133);
      }
      button {
        width: 20px;
        height: 20px;
        border: 1px solid #ff8400;
        background-color: #ff8400;
        color: #fff;
        line-height: 17px;
        text-align: center;
        font-size: 12px;
      }
    }
  }
  textarea {
    font-size: 14px;
    outline: none;
    resize: none;
    border-color: #ff8400;
    margin-top: 10px;
    padding: 5px;
    color: rgb(119, 118, 118);
  }
  textarea::placeholder{
    color: rgb(184, 184, 184);
  }
  .bottom {
    display: flex;
    justify-content: end;
    align-items: center;
    margin-top: 10px;
    .cancle {
      width: 55px;
      height: 30px;
      border: 1px solid rgb(201, 199, 199);
      color: rgb(170, 170, 170);
      line-height: 20px;
      text-align: center;
      margin-right: 10px;
      border-radius: 2px;
    }
    .cancle:hover {
      background-color: rgb(248, 248, 248);
    }
    .determine {
      width: 56px;
      height: 31px;
      color: #fff;
      background-color: #ff8400;
      line-height: 20px;
      text-align: center;
      border-radius: 2px;
    }
    .determine:hover {
      background-color: #ff9900;
    }
  }
}
</style>
